<template>
	<view class="flex-page">
		<bg-search placeholder="花名册" @search="searchHandler">
			<view class="flex align-center pt-4 font-12 text-grey1 justify-center pb-4">
				<view class="text-center">
					搜索到<text class="font-red">{{ total }}</text>人
				</view>
			</view>
		</bg-search>
		<view class="mt-3 bg-white">
			<scroll-view v-if="true" refresher-background="transparent" refresher-enabled="true"
				:refresher-triggered="triggered" @refresherrefresh="onRefresh" @scrolltolower="scrolltolower"
				style="height: calc(100vh - 288rpx);" :scroll-y="true">
				<view v-if="dataList.length > 0">
					<bg-cell2 :show-arrow="true" @click="onTap(i)" v-for="i, index in dataList" :key="index">
						<template slot="label">
							<view class="font-12">
								<text class="text-red mr-1"> [{{ i.workJobTypeName }}] </text>

								<text> {{ i.userName }} </text>
								<text>（{{ i.idCardNo }}）</text>
								&nbsp;&nbsp;
								<text class="ml-1 font-12 text-grey1" v-if="i.status == 1"> 待审批 </text>
								<text class="ml-1 text-green font-12" v-if="i.status == 2">审批通过 </text>
								<text class="ml-1 text-red font-12" v-if="i.status == 3">审批不通过 </text>

							</view>
						</template>
					</bg-cell2>
				</view>
				<u-empty v-if="dataList.length == 0 && !triggered"></u-empty>
			</scroll-view>
		</view>

	</view>
</template>

<script>
import { getEntryList } from "@/api/entry.js"
import { getNoExit } from "../../../api/project"
export default {
	onNavigationBarButtonTap(e) {
		getNoExit().then(res => {
			if (res.data && res.data.length > 0) {
				uni.showToast({
					title: "您已当前已办理入场，请勿重复操作",
					icon: "none"
				})
			} else {
				uni.navigateTo({
					url: "/pages/enter/enter/enter"
				})
			}
		})

	},

	data() {
		return {
			triggered: false,
			_freshing: false,
			params: {
				projectId: this.$store.getters.projectId,
				userName: '', pageNum: 1, pageSize: 20000,
			},
			dataList: [],
			total: 0
		}
	},
	// mounted() {
	// 	this.getData()
	// 	// uni.$on("refresh-enter", (pid) => {
	// 	// 	this.params.projectId = pid
	// 	// 	this.params.pageNum = 1
	// 	// 	this.dataList = []
	// 	// 	this.getData()
	// 	// })
	// },
	onLoad() {
		this._freshing = false;

		this.getData()
	},
	onShow() {
		// // 检查是否从详情页面返回
		// const pages = getCurrentPages()
		// const currentPage = pages[pages.length - 1]
		// const previousPage = pages[pages.length - 2]
		// console.log(currentPage);
		// console.log(previousPage);

		// 如果是从详情页面返回，则刷新数据
		// if (previousPage && previousPage.route === 'pages/enter/enter/detail') {
		this.params.pageNum = 1
		this.dataList = []
		this.getData()
		// }
	},
	methods: {
		async onRefresh() {
			if (this._freshing) return;
			this._freshing = true;
			if (!this.triggered) {
				this.triggered = true;
			}

			this.dataList = []
			this.params.pageNum = 1
			await this.getData()
			setTimeout(() => {
				this.triggered = false;
				this._freshing = false;
			}, 2000)

		},
		getData() {
			if (!this.params.projectId) {
				return false
			}
			getEntryList(this.params).then(res => {
				// this.dataList = [...this.dataList, ...res.data.rows]
				// this.total = res.data.total

				this.dataList = res.data.rows
				this.total = res.data.total
			})
		},
		searchHandler(e) {
			this.params.userName = e
			this.params.pageNum = 1
			this.dataList = []
			this.getData()
		},
		onTap(i) {
			console.log(i);
			console.log(this.$store.getters.userId);

			if (i.status == 3 && i.userId == this.$store.getters.userId) {
				// 代表自己的入场申请被拒绝了 重新提交入场申请
				this.$modal.msg('入场办理申请已被拒绝，请重新提交入场申请')
				setTimeout(() => {
					uni.navigateTo({
						url: "/pages/enter/enter/enter?id=" + i.entryId
					})
				}, 2000)

				return
			}
			uni.navigateTo({
				url: "/pages/enter/enter/detail?id=" + i.entryId
			})
		},
		scrolltolower() {
			if (this.dataList.length < this.total) {
				this.params.pageNum += 1
				this.getData()
			}
		}
	}
}
</script>

<style>
.search-reslut {
	/* padding-bottom: 30rpx; */
}
</style>
